package com.song.ejf.action;

import java.io.PrintWriter;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SResource;
import com.song.ejf.service.ResourceService;
import com.song.ejf.util.DateUtil;
import com.song.ejf.util.JSONUtil;
import com.song.ejf.util.UUIDUtil;

public class ResourceAction extends ActionSupport {

	private static final long serialVersionUID = -6609393044471160489L;
	Log log = LogFactory.getLog(ResourceAction.class);
	public SResource resource;
	public String checkItems;
	public Page page;
	@Autowired()
	public ResourceService resourceService;
	/**
	 * 用于ajax 返回所有资源为json格式 .
	 * @return
	 */
	public String loadAllRes(){
		try {
			 PrintWriter out = ServletActionContext.getResponse().getWriter();
			 out.println(JSONUtil.convertObj2Json(resourceService.loadAllRes()));
			 out.flush();
			 out.close();
		} catch (Exception e) {
			log.error("加载所有资源异常.",e);
		}
		return null;
	}
	/**
	 * 准备添加资源
	 * @return
	 */
	public String pAddResource(){
		return "pAddResource";
	}
	/**
	 * 添加资源.
	 * @return
	 */
	public String addResource(){
		try {
			if(resource != null){
				resource.setLastModifyTime(DateUtil.formatDate());
				resource.setRId(UUIDUtil.makeUUID());
				resourceService.saveOrUpdateResource(resource);
				addActionMessage("添加资源成功!");
			}else{
				addActionMessage("添加资源失败!");
			}
		} catch (Exception e) {
			log.error("添加资源异常.",e);
		}
		return "addResource";
	}
	/**
	 * 列出所有资源
	 * @return
	 */
	public String listResource(){
		try {
			if(page == null){
				page = new Page();
			}
			if(resource == null){
				resource = new SResource();
			}
			resourceService.loadResourcePage(page, resource.getResourceName(),resource.getResourceUrl());
		} catch (Exception e) {
			log.error("资源列表异常.",e);
		}
		return "listResource";
	}
	/**
	 * 删除资源
	 * @return
	 */
	public String delResource(){
		try {
			if(StringUtils.isBlank(checkItems)){
				addActionMessage("删除资源失败!");
				return "delResource";
			}
			boolean flag = resourceService.delResource(checkItems.split(","));
			if(flag){
				addActionMessage("删除资源成功!");
			}else{
				addActionMessage("删除资源失败!");
			}
		} catch (Exception e) {
			log.error("删除资源异常.",e);
		}
		return "delResource";
	}
	/**
	 * 准备编辑资源
	 * @return
	 */
	public String pEditResource(){
		try {
			resource = resourceService.findResourceById(resource.getRId());
		} catch (Exception e) {
			log.error("跳转到编辑资源页面异常.",e);
		}
		return "pEditResource";
	}
	/**
	 * 编辑资源
	 * @return
	 */
	public String editResource(){
		try {
			if(resource != null){
				resource.setLastModifyTime(DateUtil.formatDate());
				resourceService.saveOrUpdateResource(resource);
				addActionMessage("修改资源成功!");
			}else{
				addActionMessage("修改资源失败!");
			}
		} catch (Exception e) {
			log.error("编辑资源异常.",e);
		}
		return "editResource";
	}
	
	////////////////////////////////////////////////////////
	public SResource getResource() {
		return resource;
	}
	public void setResource(SResource resource) {
		this.resource = resource;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	public String getCheckItems() {
		return checkItems;
	}
	public void setCheckItems(String checkItems) {
		this.checkItems = checkItems;
	}
}
